package pl.ptemich.bumbastic;

import org.newdawn.slick.Graphics;

public class Board {

    private static final int BOARD_SIZE_X = 22;
    
    private static final int BOARD_SIZE_Y = 13;
    
    private static final int HEX_SIDE_LENGTH = 20;
    
    private int boardStartX;
    
    private int boardStartY;
    
    private Hex[][] hexes;
    
    /**
     * active / selected Hex
     */
    private Hex selectedHex;
    
    /**
     * hex highlighted by mouse pointer
     */
    private Hex hoverHex;
    
    public Board(int startX, int startY) {                
        hexes = new PointyOrientationHex[BOARD_SIZE_X][BOARD_SIZE_Y];
        double h = PointyOrientationHex.calculateH(HEX_SIDE_LENGTH);
        double r = PointyOrientationHex.calculateR(HEX_SIDE_LENGTH);
        
        this.boardStartX = startX + Double.valueOf(h + HEX_SIDE_LENGTH / 2).intValue();
        this.boardStartY = startY + Double.valueOf(r).intValue();
        
//        for (int x=0; x < BOARD_SIZE_X; x++) {
//            for (int y=0; y < BOARD_SIZE_Y; y++) {
//               if (x % 2 == 0) {
//                   hexes[x][y] = new FlatOrientationHex(HEX_SIDE_LENGTH, 
//                                         boardStartX + (h + HEX_SIDE_LENGTH) * x, 
//                                         boardStartY + 2 * r * y);
//               } else {
//                   if (y < BOARD_SIZE_Y - 1) {
//                       hexes[x][y] = new FlatOrientationHex(HEX_SIDE_LENGTH, 
//                                             boardStartX + (h + HEX_SIDE_LENGTH) * x, 
//                                             boardStartY + 2 * r * y + r);
//                   }
//               }
//            }
//        }     
        
        for (int x=0; x < BOARD_SIZE_X; x++) {
            for (int y=0; y < BOARD_SIZE_Y; y++) {
               if (y % 2 == 0) {                   
                   hexes[x][y] = new PointyOrientationHex(HEX_SIDE_LENGTH, 
                                         boardStartX + 2 * r * x, 
                                         boardStartY + (h + HEX_SIDE_LENGTH)* y,
                                         x, y);
               } else {                   
                   if (x < BOARD_SIZE_X - 1) {
                       hexes[x][y] = new PointyOrientationHex(HEX_SIDE_LENGTH, 
                                           boardStartX + 2 * r * x + r, 
                                           boardStartY + (h + HEX_SIDE_LENGTH) * y,
                                           x, y);
                   }
               }
            }
        }          
    }
    
    public void draw(Graphics graphics) {
        for (int x=0; x < BOARD_SIZE_X; x++) {
            for (int y=0; y < BOARD_SIZE_Y; y++) {
                if (hexes[x][y] != null) {
                    hexes[x][y].draw(graphics);
                }
            }
        }
        
        if (selectedHex != null) {
            graphics.drawString(selectedHex.toString(), 20, 520);
            
//            for (int x=0; x<600; x++) {
//                graphics.drawLine(x, 
//                                  Double.valueOf(selectedHex.getA1Factor() * x + selectedHex.getB1tFactor()).intValue(), 
//                                  x, 
//                                  Double.valueOf(selectedHex.getA1Factor() * x + selectedHex.getB1tFactor()).intValue());
//            }
        }
    }
    
    public void mouseClick(int posX, int posY) {
//        double r = PointyOrientationHex.calculateH(HEX_SIDE_LENGTH);
//                
//        double circle = Math.sqrt(posX * posX + posY * posY);
        for (int x=0; x < BOARD_SIZE_X; x++) {
            for (int y=0; y < BOARD_SIZE_Y; y++) {
                if (hexes[x][y] != null) {
                    //check needed because last row is not doubled
                    if (hexes[x][y].checkSelected(posX, posY)) {
                        selectedHex = hexes[x][y];
                    }
                }
            }
        }                
    }
}
